package com.lanchetech.admin.service.impl;


import com.baidu.fsg.uid.UidGenerator;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.lanchetech.admin.service.ServiceService;
import com.lanchetech.bean.request.PageReq;
import com.lanchetech.bean.response.BasePageResp;
import com.lanchetech.bean.response.BaseResp;
import com.lanchetech.bean.response.ResultData;
import com.lanchetech.bean.util.PageHelpUtil;
import com.lanchetech.bean.vo.MsgUserVO;
import com.lanchetech.common.enums.DeletedEnum;
import com.lanchetech.common.enums.MsgUserRoleEnum;
import com.lanchetech.common.enums.MsgUserStatusEnum;
import com.lanchetech.common.enums.ResultCode;
import com.lanchetech.dao.AdminMapper;
import com.lanchetech.dao.MsgUserMapper;
import com.lanchetech.entity.Admin;
import com.lanchetech.entity.MsgUser;
import com.lanchetech.entity.Shop;
import com.lanchetech.service.DaoService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class ServiceServiceImpl implements ServiceService {

    @Resource
    UidGenerator uidGenerator;

    @Autowired
    MsgUserMapper msgUserMapper;

    @Autowired
    AdminMapper adminMapper;

    @Autowired
    DaoService daoService;

    @Override
    public BaseResp editMsgUser(MsgUser msgUser) {
        MsgUser result = msgUserMapper.findOneByRelateIdAndRole(msgUser.getRelateId(), MsgUserRoleEnum.ADMIN.getRole());
        if (msgUser.getId() == null) {
            // 一个人只能绑定一个账号
            if (result != null) {
                return new BaseResp(ResultCode.MSG_USER_USERNAME_EXISTS);
            }

            Admin res = adminMapper.selectByPrimaryKey(msgUser.getRelateId());
            if (res == null) {
                return new BaseResp(ResultCode.ADMIN_NOT_EXISTS);
            }

            BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();
            msgUserMapper.insert(MsgUser.builder()
                    .username(Long.toString(uidGenerator.getUID()))
                    .password(encoder.encode(Long.toString(uidGenerator.getUID())))
                    .serviceName(msgUser.getServiceName())
                    .lastPasswordResetDate(new Date())
                    .status(msgUser.getStatus())
                    .relateId(msgUser.getRelateId())
                    .nickname("小嗨客服")
                    .avatar(msgUser.getAvatar())
                    .role(MsgUserRoleEnum.ADMIN.getRole())
                    .tenantId(0L)
                    .createdAt(new Date())
                    .updatedAt(new Date())
                    .deleted(DeletedEnum.DEFAULT.getStatus())
                    .build());
        } else {
            // 如果绑定了新管理员账号（新管理员账号没有绑定其他客服账号），或者未修改管理员账号，
            if (result == null || result.getId().equals(msgUser.getId())) {
                msgUser.setUpdatedAt(new Date());
                msgUserMapper.updateByPrimaryKeySelective(msgUser);
            }
        }

        return new BaseResp();
    }

    @Override
    public ResultData<BasePageResp<MsgUser>> getMsgUserPage(PageReq req) {
        req.setRole(MsgUserRoleEnum.ADMIN.getRole());
        Page page = PageHelper.startPage(req.getPageNo(), req.getPageSize());
        List<MsgUser> list = msgUserMapper.findAllPage(req);
        return PageHelpUtil.buildPage(list, page);
    }

    @Override
    public ResultData<MsgUser> getMsgUserDetail(Long id) {
        return new ResultData<>(msgUserMapper.selectByPrimaryKey(id));
    }

    @Override
    public BaseResp deleteMsgUser(Long id) {
        MsgUser msgUser = msgUserMapper.selectByPrimaryKey(id);
        msgUser.setRelateId(0 - msgUser.getId());
        msgUser.setStatus(MsgUserStatusEnum.OUT_SERVICE.getStatus());
        msgUser.setDeleted(DeletedEnum.DELETED.getStatus());
        msgUser.setUpdatedAt(new Date());
        msgUserMapper.updateByPrimaryKeySelective(msgUser);
        return new BaseResp();
    }
}